package io.yizoo.business.controller;

import io.elm.core.CoreController;
import io.yizoo.business.entity.*;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.EnumSet;
import java.util.List;

@RestController
@RequestMapping("/business/plm/check")
public class plm_checkController extends CoreController {

    @Override
    protected void Init() {
        PowerEntity = biz_plm_check.class;
        PowerUse = EnumSet.of(Power.IDDelete, Power.IDAny, Power.IDGetSingle,Power.IDUpsert);
    }

    @PostMapping("/GetData")
    public Object GetData(@RequestBody String jsonStr) {
        List<?> ret = Ctor.CoreMapper.USelect(
                Ctor.DMaker(jsonStr).Selector
                        .Select("t.*,a.name as plan_mc,b.name as product_mc,c.name as craft_mc,d.name as check_person_mc")
                        .From(biz_plm_check.class,"t")
                        .LeftJoin(biz_plm_plan.class,"a","a.sys_code_key=t.plan")
                        .LeftJoin(biz_plm_product.class,"b","b.sys_code_key=t.product")
                        .LeftJoin(biz_plm_craft.class,"c","c.id=t.craft")
                        .LeftJoin(biz_hrm_ei_basic.class,"d","d.emp_code=t.check_person")
                        .IsWhere("and (t.check_person like concat('%', #{name}, '%') or d.name like concat('%', #{name}, '%') )", "name")
                        .OrderBy("t.create_time desc")
                        .IsPage()
                        .GetFactor());
        return JsonResultPage(ret);
    }

    @PostMapping("/GetProduct")
    public Object GetProduct(@RequestBody String jsonStr) throws Exception {
        List<?> ret = Ctor.CoreMapper.USelect(Ctor.DMaker(jsonStr).Selector
                .Select("sys_code_key,name")
                .From(biz_plm_product.class)
                .GetFactor());
        return JsonResult(ret);
    }

    @PostMapping("/GetPlanData")
    public Object GetPlanData(@RequestBody String jsonStr) {
        List<?> ret = Ctor.CoreMapper.USelect(
                Ctor.DMaker(jsonStr).Selector
                        .From(biz_plm_plan.class)
                        .Select("name as select_name,sys_code_key as select_id")
                        .IsWhere("and (name like concat('%', #{select_name}, '%') or sys_code_key like concat('%', #{select_name}, '%'))", "select_name")
                        .IsWhere("and sys_code_key=#{select_id}", "select_id")
                        .GetFactor());
        return JsonResult(ret);
    }
}